// pages/activedetial/activedetial.js
var WxParse = require("../../wxParse/wxParse.js");
var api = require("../../api.js"),
  app = getApp();
var r;
import { $Login } from '../../components/base.js'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    article: {},
    id: '',
    isHavedCollect: ''
  },
  /**
   * 生命周期函数--监听页面加载
   *
   */
  onLoad: function(options) {
    //  console.log(options)
    var id = options.id;
    this.setData({
      id: id,
    });
    console.log('页面')
    console.log(getCurrentPages()[getCurrentPages().length - 1].route)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    $Login();
    wx.showLoading({
      title: "数据加载中"
    });
    let sendData = {
      art_id:this.data.id,
      open_id: getApp().globalData.wxUserInfo.openId
    }
    wx.request({
      url: api.details.artDetail,
      data: sendData,
      header: {
        "content-type": "application/json"
      },
      method: 'GET',
      success(res) {
        setTimeout(() => {
          wx.hideLoading()
          const content = res.data.data.article.artDesc;
          res.data.data.article.artDesc = that.formatRichText(content);
          that.setData({
            article: res.data.data.article,
            isHavedCollect: res.data.data.article.isHavedCollect
          })
        },500)
      }
    });
  },
  formatRichText: function (html) {
    let newContent = html.replace(/<img[^>]*>/gi, function (match, capture) {
      match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
      match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
      match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
      return match;
    });
    newContent = newContent.replace(/style="[^"]+"/gi, function (match, capture) {
      match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
      return match;
    });
    newContent = newContent.replace(/<br[^>]*\/>/gi, '');
    newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"');
    return newContent;
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.showNavigationBarLoading(),
      console.log("下拉刷新"),
      this.onShow(),
      setTimeout(function() {
        wx.hideNavigationBarLoading(), wx.stopPullDownRefresh();
      }, 1500);
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (type) {
    return {
      title: this.data.article.artTitle,
      path: `pages/art/art?id=${this.data.article.artId}`,
      success: function (res) {
      },
      fail: function (res) {
      }
     }
  },
  follow: function() {
    let that = this;
    if (!app.globalData.wxUserInfo.openId) {
      wx.showToast({
        title: '请登录',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (this.data.isHavedCollect == 0) {
      let sendData = {
        otherId: this.data.id,
        type: 0,
        openId: getApp().globalData.wxUserInfo.openId,
        img: this.data.article.img,
        title: this.data.article.artTitle
      }
      wx.request({
        url: api.follow.addFollow,
        data: sendData,
        method: "POST",
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        success(res) {
          if (res.data.code == 200) {
            wx.showToast({
              title: '已收藏',
              image: '../../images/col.png',
              duration: 2000
            })
            that.setData({
              isHavedCollect: 1
            })
          }
        }
      });
    } else if (this.data.isHavedCollect == 1) {
      let sendData = {
        other_id: this.data.id,
        open_id: getApp().globalData.wxUserInfo.openId
      }
      wx.request({
        url: api.follow.delFollow,
        data: sendData,
        method: "POST",
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        success(res) {
          if (res.data.code == 200) {
            that.setData({
              isHavedCollect: 0
            })
            wx.showToast({
              title: '已取消',
              image: '../../images/unclo_03.png',
              duration: 2000
            })
          }
        }
      });
    }
  }
});
